﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;

namespace Hangout.Client.Core.Location
{
    public class CurrentLocation
    {


        public CurrentLocation()
        {
            UpdateToServer = true;
        }


        public delegate void StatusChangedHandler(GeoPositionStatusChangedEventArgs e);
        public delegate void UserLocationUpdateCompletedHandler(System.ComponentModel.AsyncCompletedEventArgs e);
        public delegate void PositionChangedHandler(GeoPositionChangedEventArgs<GeoCoordinate> e);

        public  event StatusChangedHandler StatusChanged;

        public  event UserLocationUpdateCompletedHandler UserLocationUpdateCompleted;

        public  event PositionChangedHandler PositionChanged;

        public  GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

         

        public  bool UpdateToServer { get; set; }

        public  void StartWatcher()
        {
            try
            {
                AttachEvents();
                watcher.Start();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private  void AttachEvents()
        {
            try
            {
                watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        public  void StopWatcher()
        {
            try
            {
                DetachEvents();
                watcher.Stop();
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        public  void DetachEvents()
        {
            try
            {
                watcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

       

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            try
            {

                if (StatusChanged != null)
                {
                    StatusChanged(e);
                }

            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }


        }

         void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            try
            {
                if (watcher.Status == GeoPositionStatus.Ready)
                {



                    //connect to the server here and update the position :)
                   
                    UserLocationService.Location location = new UserLocationService.Location();
                    if (double.IsNaN(e.Position.Location.Latitude))
                    {
                        return;
                    }
                    location.Latitude = e.Position.Location.Latitude;

                    if (double.IsNaN(e.Position.Location.Longitude))
                    {
                        return;
                    }


                    location.Longitude = e.Position.Location.Longitude;
                    
                    location.UserID = Settings.Settings.UserID;
                    if (UpdateToServer)
                    {
                        Services.UserLocationServiceClient.UpdateUserLocationCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UpdateUserLocationCompleted);
                        Services.UserLocationServiceClient.UpdateUserLocationAsync(location, Settings.Settings.FacebookAccessToken);
                    }

                    if (PositionChanged != null)
                    {
                        PositionChanged(e);
                    }

                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

         void client_UpdateUserLocationCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            try
            {
                if (UserLocationUpdateCompleted != null)
                {
                    UserLocationUpdateCompleted(e);
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }






       
    }
}
